﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;

namespace HdipCricketClub.Controllers
{ 
    public class OpponentController : Controller
    {
        private CricketClubEntities db = new CricketClubEntities();

        OpponentRepository opponentRepository = new OpponentRepository();

        //
        // GET: Opponent/List/

        public ViewResult List(string sortOrder, string searchString)
        {
            ViewBag.OpponentSortParm = sortOrder;
            var opponents = opponentRepository.FindAllOpponents();
                        
            if (!String.IsNullOrEmpty(searchString))
            {
                opponents = opponents.Where(f => f.club_name.ToUpper().Contains(searchString.ToUpper()));
            }
            return View(opponents);
        }
        
        //
        // GET: /Opponent/

        public ViewResult Index()
        {
            var opponents = opponentRepository.FindAllOpponents();
            return View(opponents);
        }

        //
        // GET: /Opponent/Details/5

        public ViewResult Details(int id)
        {
            Opponent opponent = opponentRepository.GetOpponent(id);
            return View(opponent);
        }

        //
        // GET: /Opponent/Create

        public ActionResult Create()
        {
            ViewBag.county = new SelectList(db.Counties, "county_id", "name");
            return View();
        } 

        //
        // POST: /Opponent/Create

        [HttpPost]
        public ActionResult Create(Opponent opponent)
        {
            if (ModelState.IsValid)
            {
                db.Opponents.AddObject(opponent);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.county = new SelectList(db.Counties, "county_id", "name", opponent.county);
            return View(opponent);
        }
        
        //
        // GET: /Opponent/Edit/5
 
        public ActionResult Edit(int id)
        {
            Opponent opponent = opponentRepository.GetOpponent(id);
            ViewBag.county = new SelectList(db.Counties, "county_id", "name", opponent.county);
            return View(opponent);
        }

        //
        // POST: /Opponent/Edit/5

        [HttpPost]
        public ActionResult Edit(Opponent opponent)
        {
            if (ModelState.IsValid)
            {
                db.Opponents.Attach(opponent);
                db.ObjectStateManager.ChangeObjectState(opponent, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.county = new SelectList(db.Counties, "county_id", "name", opponent.county);
            return View(opponent);
        }

        //
        // GET: /Opponent/Delete/5
 
        public ActionResult Delete(int id)
        {
            Opponent opponent = opponentRepository.GetOpponent(id);
            return View(opponent);
        }

        //
        // POST: /Opponent/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Opponent opponent = db.Opponents.Single(o => o.opponent_id == id);
                db.Opponents.DeleteObject(opponent);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                Opponent opponent = db.Opponents.Single(o => o.opponent_id == id);
                return RedirectToAction("UpdateError", new { id = opponent.opponent_id });
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        // GET:  /Opponent/UpdateError

        public ActionResult UpdateError(int id)
        {
            Opponent opponent = db.Opponents.Single(o => o.opponent_id == id);

            return View(opponent);
        }



    }
}